/*
 * @(#)Result.java 2009-3-25
 * 
 * Copyright 5jxiang . All rights reserved.
 */
package com.jz.util;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * <a href="Result.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author 5jxiang
 * @version $Id$
 */
public class Result implements Serializable {
	//~ Static fields/initializers =============================================

	//~ Instance fields ========================================================

	private int offset;
    private int size;
    private int total;

    private List data;
    
    private List requestedData;

    //~ Constructors ===========================================================
    
    public Result() {
    }
    
    public Result(int offset, int size) {
        this.offset = offset;
        this.size = size;
    }

    //~ Methods ================================================================

    public boolean isFirst() {
    	return offset == 0;
    }
    
    public boolean isLast() {
    	return !hasNext();
    }
    
    public boolean hasNext() {
    	if (data == null || size < 0) {
    		return false;
    	}
    	return data.size() > size;
    }
    
    public int getTotalPage() {
    	if (size < 0) return 1;
    	
    	return total % size == 0 ? total/size : total/size + 1;
    }
    
    public int getPage() {
    	return offset/size + 1;
    }
    
    //~ Accessors ==============================================================

    public void setData(List data) {
        this.data = data;
        this.requestedData = null;
    }

    public List getData() {
    	if (requestedData == null) {
    		if (size < 0 || data.size() <= size) {
    			requestedData = data;
    		} else {
    			requestedData = data.subList(0, size);
    		}
    	}
    	return requestedData;
    }

    public List getOriginalData() {
    	return data;
    }

    /**
	 * @return Returns the offset.
	 */
	public int getOffset() {
		return offset;
	}
	/**
	 * @param offset The offset to set.
	 */
	public void setOffset(int offset) {
		this.offset = offset;
	}
    /**
     * @return Returns the size.
     */
    public int getSize() {
        return size;
    }
    /**
     * @param size The size to set.
     */
    public void setSize(int size) {
        this.size = size;
    }
    /**
     * @return Returns the total.
     */
    public int getTotal() {
        return total;
    }
    /**
     * @param total The total to set.
     */
    public void setTotal(int total) {
        this.total = total;
    }
}


